﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Configuration;
using Ivy.Common.Framework.GeneralUtility;
using Ivy.Common.Framework.Configuration;

namespace Ivy.Common.Framework.EmailUtility
{
    public enum EmailAddressType
    {
        SendTo = 0,
        SendCC = 1,
        SendBcc = 2,
        ReplyTo = 3
    }

    public class EmailUtility
    {
        #region Variables & Constants
        MailMessage _MailMessage;
        const string _FrameworkIvyConfigSection = "FrameworkIvyConfigSection";
        const string _DefaultSmtpServer = "DefaultSmtpServer";

        const string _AlertSenderDefaultEmail = "AlertSenderDefaultEmail";
        const string _AlertsenderDefaultDisplayName = "AlertsenderDefaultDisplayName";
        const string _AlertReceiverDefaultEmails = "AlertReceiverDefaultEmails";
        #endregion Variables & Constants

        #region Constructor & Destructor
        #endregion Constructor & Destructor

        #region Public Properties
        #endregion Public Properties

        #region Public Methods
        public SmtpClient GetSmptClient()
        {
            SmtpClient lSmtpClient;
            AppSettingsReader _AppSettingsReader = new AppSettingsReader();
            string _DefaultSmtpServerName = _AppSettingsReader.GetValue(_DefaultSmtpServer, typeof(string)).ToString();
            SmtpServer _SmtpServer = (((FrameworkIvyConfigSection)ConfigurationManager.GetSection(_FrameworkIvyConfigSection)).SmtpMailServers[_DefaultSmtpServerName]);
            lSmtpClient = CreateSmtpClientInstance(_SmtpServer);
            return lSmtpClient;
        }
        public SmtpClient GetSmptClient(string AppSettingsConfigKeyForSmtpServer)
        {
            SmtpClient lSmtpClient;
            SmtpServer _SmtpServer = (((FrameworkIvyConfigSection)ConfigurationManager.GetSection(_FrameworkIvyConfigSection)).SmtpMailServers[AppSettingsConfigKeyForSmtpServer]);
            lSmtpClient = CreateSmtpClientInstance(_SmtpServer);
            return lSmtpClient;
        }
        public SmtpClient GetSmptClient(string smtpHost, string smtpPort, string smtpUserId, string smtpUserPassword, bool isPasswordEncrypted, bool isSSLEnabled, bool isUseDefaultCredentials)
        {
            SmtpClient lSmtpClient = new SmtpClient();

            if (string.IsNullOrEmpty(smtpHost))
            {
                if (string.IsNullOrEmpty(smtpPort))
                {
                    if (string.IsNullOrEmpty(smtpUserId))
                    {
                        if (string.IsNullOrEmpty(smtpUserPassword))
                        {
                            lSmtpClient.Host = smtpHost;
                            lSmtpClient.Port = Int32.Parse(smtpPort);
                            lSmtpClient.UseDefaultCredentials = isUseDefaultCredentials;
                            if (isPasswordEncrypted)
                            {
                                lSmtpClient.Credentials = new System.Net.NetworkCredential(smtpUserId, StringUtility.DecryptString(smtpUserPassword));
                            }
                            else
                            {
                                lSmtpClient.Credentials = new System.Net.NetworkCredential(smtpUserId, smtpUserPassword);
                            }
                            lSmtpClient.EnableSsl = isSSLEnabled;
                        }
                    }
                }
            }
            return lSmtpClient;
        }

        public string[] GetDefaultAlertMailAdresses()
        {
            string[] lAlertMailAddresses;
            AppSettingsReader _AppSettingsReader = new AppSettingsReader();
            string lAlertReceiverDefaultEmails = _AppSettingsReader.GetValue(_AlertReceiverDefaultEmails, typeof(string)).ToString();
            lAlertMailAddresses = lAlertReceiverDefaultEmails.Split(';');
            return lAlertMailAddresses;
        }
        public string GetComaSeperatedDefaultAlertMailAddresses()
        {
            string lComaSeperatedDefaultMailAddresses = string.Empty;
            AppSettingsReader _AppSettingsReader = new AppSettingsReader();
            lComaSeperatedDefaultMailAddresses = _AppSettingsReader.GetValue(_AlertReceiverDefaultEmails, typeof(string)).ToString();
            return lComaSeperatedDefaultMailAddresses;
        }
        
        public void AddEmailSenderAddressToMailMessage(MailMessage mailMessageObject)
        {
            AppSettingsReader _AppSettingsReader = new AppSettingsReader();
            string lAlertSenderDefaultEmail = _AppSettingsReader.GetValue(_AlertSenderDefaultEmail, typeof(string)).ToString();
            string lAlertsenderDefaultDisplayName = _AppSettingsReader.GetValue(_AlertsenderDefaultDisplayName, typeof(string)).ToString();
            if (!string.IsNullOrEmpty(lAlertSenderDefaultEmail.Trim()))
            {
                mailMessageObject.From = new MailAddress(lAlertSenderDefaultEmail, lAlertsenderDefaultDisplayName);
            }
        }
        public void AddEmailSenderAddressToMailMessage(MailMessage mailMessageObject, string emailAddress)
        {
            if (!string.IsNullOrEmpty(emailAddress))
            {
                mailMessageObject.From = new MailAddress(emailAddress);
            }
        }
        public void AddEmailSenderAddressToMailMessage(MailMessage mailMessageObject, string emailAddress, string emailDisplayName)
        {
            if (!string.IsNullOrEmpty(emailAddress))
            {
                mailMessageObject.From = new MailAddress(emailAddress, emailDisplayName);
            }
        }
        public void AddEmailRecepientOrReplyAddressesToMailMessage(MailMessage mailMessageObject, EmailAddressType emailAddressType, string semicolonSeperatedEmailAddresses)
        {
            string[] lEmailAddresses = semicolonSeperatedEmailAddresses.Split(';');
            foreach (string lEmailAddress in lEmailAddresses)
            {
                if (!string.IsNullOrEmpty(lEmailAddress))
                {
                    if (emailAddressType == EmailAddressType.SendTo)
                    {
                        mailMessageObject.To.Add(lEmailAddress);
                    }
                    if (emailAddressType == EmailAddressType.SendCC)
                    {
                        mailMessageObject.CC.Add(lEmailAddress);
                    }
                    if (emailAddressType == EmailAddressType.SendBcc)
                    {
                        mailMessageObject.Bcc.Add(lEmailAddress);
                    }
                }
            }
        }
        public void AddEmailRecepientForAdminGroupToMailMessage(MailMessage mailMessageObject)
        {
            AppSettingsReader _AppSettingsReader = new AppSettingsReader();
            string lAlertReceiverDefaultEmails = _AppSettingsReader.GetValue(_AlertReceiverDefaultEmails, typeof(string)).ToString();
            string[] lEmailAddresses = lAlertReceiverDefaultEmails.Split(';');
            foreach (string lEmailAddress in lEmailAddresses)
            {
                if (!string.IsNullOrEmpty(lEmailAddress))
                {
                    mailMessageObject.Bcc.Add(lEmailAddress);
                }
            }
    }
        
        public void SendEmail(MailMessage mailMessageObject)
        {
            SendEmail(GetSmptClient(), mailMessageObject);
        }
        public void SendEmail(string AppSettingsConfigKeyForSmtpServer, MailMessage mailMessageObject)
        {
            SendEmail(GetSmptClient(AppSettingsConfigKeyForSmtpServer), mailMessageObject);
        }
        public void SendEmail(SmtpClient smtpClientObject, MailMessage mailMessageObject)
        {
            if (mailMessageObject.From != null)
            {
                if (!(mailMessageObject.To == null|| mailMessageObject.CC == null || mailMessageObject.Bcc == null))
                {
                    smtpClientObject.Send(mailMessageObject);
                }
            }
        }
        #endregion Public Methods

        #region Events
        #endregion Events

        #region Private Methods
        private SmtpClient CreateSmtpClientInstance(SmtpServer pSmtpServerConfiguration)
        {
            SmtpClient lSmtpClient = new SmtpClient();
            if (pSmtpServerConfiguration != null)
            {
                if (!string.IsNullOrEmpty(pSmtpServerConfiguration.SmtpHost))
                {
                    if (!string.IsNullOrEmpty(pSmtpServerConfiguration.SmtpPort))
                    {
                        lSmtpClient.Host = pSmtpServerConfiguration.SmtpHost;
                        lSmtpClient.Port = Int32.Parse(pSmtpServerConfiguration.SmtpPort);
                        lSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                        //if (bool.Parse(pSmtpServerConfiguration.IsUseDefaultCredentials))
                        //{
                            lSmtpClient.UseDefaultCredentials = bool.Parse(pSmtpServerConfiguration.IsUseDefaultCredentials);
                            if (!string.IsNullOrEmpty(pSmtpServerConfiguration.SmtpAuthUserId))
                            {
                                if (!string.IsNullOrEmpty(pSmtpServerConfiguration.SmtpAuthUserPassword))
                                {
                                    if (!string.IsNullOrEmpty(pSmtpServerConfiguration.IsHostSSLEnabled))
                                    {
                                        if (!string.IsNullOrEmpty(pSmtpServerConfiguration.IsPasswordEncrypted))
                                        {
                                            if (bool.Parse(pSmtpServerConfiguration.IsPasswordEncrypted))
                                            {
                                                lSmtpClient.Credentials = new System.Net.NetworkCredential(pSmtpServerConfiguration.SmtpAuthUserId, StringUtility.DecryptString(pSmtpServerConfiguration.SmtpAuthUserPassword));
                                            }
                                            else
                                            {
                                                lSmtpClient.Credentials = new System.Net.NetworkCredential(pSmtpServerConfiguration.SmtpAuthUserId, pSmtpServerConfiguration.SmtpAuthUserPassword);
                                            }
                                        }
                                        else
                                        {
                                            {
                                                lSmtpClient.Credentials = new System.Net.NetworkCredential(pSmtpServerConfiguration.SmtpAuthUserId, pSmtpServerConfiguration.SmtpAuthUserPassword);
                                            }
                                        }
                                        lSmtpClient.EnableSsl = bool.Parse(pSmtpServerConfiguration.IsHostSSLEnabled);
                                    }
                                }
                            }
                        //}
                    }
                }
            }
            return lSmtpClient;
        }
        #endregion Private Methods
    }
}
